import 'package:getx_demo/common/utils/utils.dart';
import 'package:getx_demo/common/values/assets.dart';
import 'package:flutter/material.dart';

class CommonAppBar extends AppBar {
  /// 导航栏背景色是否是白色，默认false
  final bool isWithBgColor;

  /// 是否包含自定义返回按钮，默认true
  final bool isBackLeading;

  ///距离左边按钮距离
  // final double distanceLeftBtnWidth;
  final VoidCallback? leadingOnPress;

  CommonAppBar({
    super.key,
    super.title,
    super.actions,
    super.leading,
    super.foregroundColor,
    super.centerTitle = true,
    super.titleSpacing,
    super.bottom,
    this.isWithBgColor = false,
    this.isBackLeading = true,
    this.leadingOnPress,
  });
  @override
  State<CommonAppBar> createState() => _CommonAppBarState();
}

class _CommonAppBarState extends State<CommonAppBar> {
  @override
  Widget build(BuildContext context) {
    Color bgColor = widget.isWithBgColor ? Colors.white : '#0E67E7'.toColor();
    Color? foregroundColor = widget.foregroundColor;
    foregroundColor ??= widget.isWithBgColor ? Colors.black : Colors.white;
    Widget? leading = widget.leading;
    if (widget.isBackLeading && leading == null) {
      leading = TextButton(
        onPressed: () {
          if (widget.leadingOnPress != null) {
            widget.leadingOnPress!();
          } else {
            Navigator.of(context).pop();
          }
        },
        child: Image.asset(
          // 'img/erds/common/editor_common_back.png',
          Assets.commonEditorCommonBack,
          width: 10,
          height: 19,
          color: widget.isWithBgColor ? null : Colors.white,
        ),
      );
    }
    return AppBar(
      leading: leading,
      title: widget.title,
      actions: widget.actions,
      foregroundColor: foregroundColor,
      backgroundColor: bgColor,
      centerTitle: widget.centerTitle,
      elevation: 0,
      titleSpacing: widget.titleSpacing,
      bottom: widget.bottom,
    );
  }
}
